using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using Utils.Explorer;
using MyCollections.Model;
using MyCollections.Controls;
using Utils.XML;


namespace MyCollections.Forms
{
    public partial class fFindFolderMovies_v1 : Form
    {
        public fFindFolderMovies_v1()
        {
            InitializeComponent();
            //set mode type to MULTIPLE
            this.listViewMovie.setMode(this.listViewMovie.MULTIPLE);
        }

        public fFindFolderMovies_v1(fMain parent):this()
        {
            this.MdiParent = parent;
            this.listViewMovie.MdiParent = parent;

            this.bt_save_all.Enabled = false;

            //TEMP
            this.tb_path.Text = @"E:\MyCollections tests";
        }

        #region EVENTS
        // ++++++++++++++++++++++++++++++++++++++++++++++++++++
        private void bt_browse_Click(object sender, EventArgs e)
        {
            if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                tb_path.Text = this.folderBrowserDialog1.SelectedPath;
        }

        private void bt_search_Click(object sender, EventArgs e)
        {
            if (!Directory.Exists(this.tb_path.Text))
                return;

            bool results = false;
            Movie[] moviesResult = null;

            //clear before data
            this.listViewMovie.RemoveAllItens();

            // Load list of movies
            string[] titles = findFolderMovies(this.tb_path.Text);
            MovieCollection[] movies = new MovieCollection[titles.Length];
            for(int i = 0; i<titles.Length; i++)
            {
                movies[i] = new MovieCollection();
                movies[i].Title = titles[i];
                movies[i].Validate = false;            
            }

            if (titles.Length > 0)
                results = true;

            // fill the ListBoxMovie with only title
            this.listViewMovie.FillListbox(movies);
            this.listViewMovie.Gb_results.Enabled = results;
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            /*

            Movie[] m = new Movie[3];            
            Movie a =new Movie();
            a.Title = "ajklshdjashdjas title";
            a.Tagline = "askldjaslkjsdkldj tagline";
            a.Year = 1982;
            Image i = Image.FromFile(@"G:\Projectos\MyCollections\trunk\Stuff\MV5BMjE1ODM3NDk4MV5BMl5BanBnXkFtZTcwNjg0NjYyMQ@@._V1._SX100_SY140_.jpg");
            Bitmap b = new Bitmap(i);
            //a.Image = i;
            a.ImageURL = "http://www.abola.pt";
            m[0] = a;

            a.Title = "title a";
            m[1] = a;
            a.Title = "title b";
            m[2] = a;

            // test 
            XML.SaveFile("C:\\collection.xml", b);*/
            //Movie[] c = (Movie[])XML.LoadFile("C:\\collection.xml", typeof(Movie[]));
        }

        private void bt_save_all_Click(object sender, EventArgs e)
        {
            //TODO:
            // load configurations
            // save this.listViewMovie.Itens 
        }

        // ----------------------------------------------------
        #endregion

        private string[] findFolderMovies(string path)
        {
            ArrayList completFileList = Tasks.GenerateFileList(path, true);

            // TODO: add file types to config
            ArrayList file_types = new ArrayList();
            file_types.Add(".avi");

            ArrayList selectedFileList = Tasks.FilterFileList(completFileList, file_types);
            ArrayList movies = Files.GetFileNames(selectedFileList);

            return (string[])movies.ToArray(typeof(string));
        }

        private void listViewMovie_Load(object sender, EventArgs e)
        {

        }
    }
}